package com.pzhu.dormitory.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.pzhu.dormitory.entity.BuildingEntity;
import com.pzhu.dormitory.vo.RoomVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.pzhu.dormitory.entity.RoomEntity;
import com.pzhu.dormitory.service.RoomService;
import com.pzhu.dormitory.utils.PageUtils;
import com.pzhu.dormitory.utils.Rb;
import com.pzhu.dormitory.utils.R;


/**
 * 寝室
 *
 * @author
 * @email
 * @date 2021-05-02 15:20:05
 */
@Slf4j
@RestController
@Api(value = "RoomController", tags = "寝室")
@RequestMapping("dormitory/room")
public class RoomController {
    @Autowired
    private RoomService roomService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "获取寝室list")
    public R<RoomEntity> list(@RequestParam Map<String, Object> params) {
        PageUtils<RoomEntity> page = roomService.queryPage(params);
        return Rb.a(RoomEntity.class).success().page(page).build();
    }

    /**
     * 列表
     */
    @GetMapping("/page")
    @ApiOperation(value = "获取寝室list")
    public R<RoomVo> page(@RequestParam Map<String, Object> params) {
        PageUtils<RoomVo> page = roomService.queryPageRoomVo(params);
        return Rb.a(RoomVo.class).success().page(page).build();
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "获取一个寝室信息")
    public R<RoomEntity> info(@Validated @PathVariable("id") Long id) {
            RoomEntity room = roomService.getById(id);
        return Rb.a(RoomEntity.class).success().withData(room).build();
    }

    /**
     * 保存
     */
    @ApiOperation(value = "保存寝室信息")
    @PostMapping("/save")
    public R<RoomEntity> save(@Validated @RequestBody RoomEntity room) {
            roomService.save(room);
        return Rb.a(RoomEntity.class).success().build();
    }

    /**
     * 修改
     */
    @ApiOperation(value = "更新寝室信息")
    @PutMapping("/update")
    public R<RoomEntity> update(@RequestBody RoomEntity room) {
            roomService.updateById(room);
        return Rb.a(RoomEntity.class).success().build();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除寝室信息")
    @DeleteMapping("/delete")
    public R<RoomEntity> delete(@RequestBody Long[] ids) {
            roomService.removeByIds(Arrays.asList(ids));
        return Rb.a(RoomEntity.class).success().build();
    }

    @GetMapping("/options")
    @ApiOperation(value = "获取list")
    public R<RoomEntity> options(Long buildingId ) {
        List<RoomEntity> list = roomService.lambdaQuery().eq(RoomEntity::getBuildingId,buildingId).list();
        return Rb.a(RoomEntity.class).success().list(list).build();
    }

}
